package com.jz.pay3.design.command;


import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class CommandController {
    Map<Object,Command> map = new HashMap<>();
    public CommandController() { }
    public CommandController(Command command) {
        addCommand(command);
    }
    public boolean addCommand(Command command){
        if (!StringUtils.isEmpty(command) && !StringUtils.isEmpty(command.getCmd())){
            map.put(command.getCmd() ,command);
            return command.equals(map.get(command.getCmd()));
        }
        return false;
    }
    public boolean removeCommand(Command command){
        if (!StringUtils.isEmpty(command) && !StringUtils.isEmpty(command.getCmd())){
            return null != map.remove(command.getCmd());
        }
        return false;
    }

    public void sendCmd(Object key){
        Command command = map.get(key);
        if (null != command && null != command.getCmd()){
            command.excute();
        }
    }
    public void sendCmd(Command cmd){
        Object key = null;
        if (null != cmd){
            key = cmd.getCmd();
        }
        Command command = map.get(key);
        if (null != command && null != command.getCmd()){
            command.excute();
        }
    }
    public void sendBatchCmd(){
        for (Map.Entry<Object, Command> objectCommandEntry : map.entrySet()) {
            objectCommandEntry.getValue().excute();
        }
    }
}
